package hu.elte.inf.holiuml.coordgeom;

/**
 * Represents a free vector with two coordinates.
 * 
 * It is an immutable object.
 * @author Csaba Hoch
 */
public class CGVector {

	private int x;
	private int y;

	public CGVector(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public CGVector(CGPoint p1, CGPoint p2) {
		this.x = p2.getX() - p1.getX();
		this.y = p2.getY() - p1.getY();
	}

	public CGVector getRotatedPositive() {
		return new CGVector(y, -x);
	}

	public CGVector getRotatedNegative() {
		return new CGVector(-y, x);
	}

	public CGVector getFlipped() {
		return new CGVector(-x, -y);
	}
	
	public int scalarProduct(CGVector v) {
		return x * v.x + y * v.y;
	}

	public int getLength() {
		return (int) (Math.sqrt(x * x + y * y));
	}
	
	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

	public String toString() {
		return "("+x+","+y+")";
	}
}
